home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_190 / nethack / twee.zoo / topten.c < prev    next >
C/C++ Source or Header  |  1988-08-04  |  13KB  |  520 lines

  1. /*    SCCS Id: @(#)topten.c   2.3     88/02/01
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3.  
  4. #include <stdio.h>
  5. #include "hack.h"
  6. #ifdef GENIX
  7. #define void    int    /* jhn - mod to prevent compiler from bombing */
  8. #endif
  9.  
  10. #define Sprintf (void) sprintf
  11. extern char plname[], pl_character[];
  12. #ifndef MSC        /* set by the Microsoft "C" compiler */
  13. extern    char    *itoa();
  14. #endif
  15. extern    char    *ordin(), *eos();
  16. extern int done_hup, done_stopprint;
  17.  
  18. #define newttentry() (struct toptenentry *) alloc(sizeof(struct toptenentry))
  19. #define NAMSZ    10
  20. #define DTHSZ    60
  21. #define PERSMAX  3        /* entries per name/uid per char. allowed */
  22. #define POINTSMIN    1    /* must be > 0 */
  23. #define ENTRYMAX    100    /* must be >= 10 */
  24. #ifndef MSDOS
  25. #define PERS_IS_UID        /* delete for PERSMAX per name; now per uid */
  26. #endif
  27. struct toptenentry {
  28.     struct toptenentry *tt_next;
  29.     long int points;
  30.     int level,maxlvl,hp,maxhp;
  31.     int uid;
  32.     char plchar;
  33.     char sex;
  34.     char name[NAMSZ+1];
  35.     char death[DTHSZ+1];
  36.     char date[7];        /* yymmdd */
  37. } *tt_head;
  38.  
  39. topten(){
  40.     int uid = getuid();
  41.     int rank, rank0 = -1, rank1 = 0;
  42.     int occ_cnt = PERSMAX;
  43.     register struct toptenentry *t0, *t1, *tprev;
  44.     char *recfile = RECORD;
  45. #ifdef UNIX
  46.     char *reclock = "record_lock";
  47. #endif
  48.     int sleepct = 300;
  49.     FILE *rfile;
  50.     register flg = 0;
  51.     extern char *getdate();
  52. #ifdef LOGFILE
  53.     char *lgfile = LOGFILE;
  54.     FILE *lfile;
  55.     char *loglock = "logfile_lock";
  56.     int sleeplgct = 30;
  57. #endif
  58.  
  59. #ifdef AMIGA
  60.     curx = FAR; /* Prevent screen from closing too fast */
  61. #endif
  62.  
  63. #ifndef DGK
  64. #define HUP    if(!done_hup)
  65. #else
  66. #define HUP
  67. #endif
  68.  
  69. #ifdef UNIX
  70.     while(link(recfile, reclock) == -1) {
  71.         HUP perror(reclock);
  72.         if(!sleepct--) {
  73.             HUP puts("I give up. Sorry.");
  74.             HUP puts("Perhaps there is an old record_lock around?");
  75.             return;
  76.         }
  77.         HUP printf("Waiting for access to record file. (%d)\n",
  78.             sleepct);
  79.         HUP (void) fflush(stdout);
  80.         sleep(1);
  81.     }
  82. #endif
  83.     if(!(rfile = fopen(recfile,"r"))){
  84.         HUP puts("Cannot open record file!");
  85.         goto unlock;
  86.     }
  87.     HUP (void) putchar('\n');
  88.  
  89.     /* create a new 'topten' entry */
  90.     t0 = newttentry();
  91.     t0->level = dlevel;
  92.     t0->maxlvl = maxdlevel;
  93.     t0->hp = u.uhp;
  94.     t0->maxhp = u.uhpmax;
  95.     t0->points = u.urexp;
  96.     t0->plchar = pl_character[0];
  97.     t0->sex = (flags.female ? 'F' : 'M');
  98.     t0->uid = uid;
  99.     (void) strncpy(t0->name, plname, NAMSZ);
  100.     (t0->name)[NAMSZ] = 0;
  101.     (void) strncpy(t0->death, killer, DTHSZ);
  102.     (t0->death)[DTHSZ] = 0;
  103.     (void) strcpy(t0->date, getdate());
  104.  
  105.     /* assure minimum number of points */
  106.     if(t0->points < POINTSMIN)
  107.         t0->points = 0;
  108. #ifdef LOGFILE        /* used for debugging (who dies of what, where) */
  109. # ifdef UNIX
  110.     while(link(lgfile, loglock) == -1) {
  111.         HUP perror(loglock);
  112.         if(!sleeplgct--) {
  113.             HUP puts("I give up. Sorry.");
  114.             HUP puts("Perhaps there is an old logfile_lock around?");
  115.             goto lgend;
  116.         }
  117.         HUP printf("Waiting for access to log file. (%d)\n",
  118.             sleeplgct);
  119.         HUP (void) fflush(stdout);
  120.         sleep(1);
  121.     }
  122. # endif
  123.     if(!(lfile = fopen(lgfile,"a"))){
  124.         HUP puts("Cannot open log file!");
  125.         goto lgend;
  126.     }
  127.     fprintf(lfile,"%6s %d %d %d %d %d %ld %c%c %s,%s\n",
  128.         t0->date, t0->uid,
  129.         t0->level, t0->maxlvl,
  130.         t0->hp, t0->maxhp, t0->points,
  131.         t0->plchar, t0->sex, t0->name, t0->death);
  132.     fclose(lfile);
  133. # ifdef UNIX
  134.     (void) unlink(loglock);
  135. # endif
  136.       lgend:;
  137. #endif
  138.  
  139.     t1 = tt_head = newttentry();
  140.     tprev = 0;
  141.     /* rank0: -1 undefined, 0 not_on_list, n n_th on list */
  142.     for(rank = 1; ; ) {
  143.       if(fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
  144.         t1->date, &t1->uid,
  145.         &t1->level, &t1->maxlvl,
  146.         &t1->hp, &t1->maxhp, &t1->points,
  147.         &t1->plchar, &t1->sex, t1->name, t1->death) != 11
  148.       || t1->points < POINTSMIN)
  149.             t1->points = 0;
  150.       if(rank0 < 0 && t1->points < t0->points) {
  151.         rank0 = rank++;
  152.         if(tprev == 0)
  153.             tt_head = t0;
  154.         else
  155.             tprev->tt_next = t0;
  156.         t0->tt_next = t1;
  157.         occ_cnt--;
  158.         flg++;        /* ask for a rewrite */
  159.       } else
  160.         tprev = t1;
  161.       if(t1->points == 0) break;
  162.       if(
  163. #ifdef PERS_IS_UID
  164.          t1->uid == t0->uid &&
  165. #else
  166.          strncmp(t1->name, t0->name, NAMSZ) == 0 &&
  167. #endif
  168.          t1->plchar == t0->plchar && --occ_cnt <= 0){
  169.         if(rank0 < 0){
  170.             rank0 = 0;
  171.             rank1 = rank;
  172.     HUP printf("You didn't beat your previous score of %ld points.\n\n",
  173.                 t1->points);
  174.         }
  175.         if(occ_cnt < 0){
  176.             flg++;
  177.             continue;
  178.         }
  179.       }
  180.       if(rank <= ENTRYMAX){
  181.         t1 = t1->tt_next = newttentry();
  182.         rank++;
  183.       }
  184.       if(rank > ENTRYMAX){
  185.         t1->points = 0;
  186.         break;
  187.       }
  188.     }
  189.     if(flg) {       /* rewrite record file */
  190.         (void) fclose(rfile);
  191.         if(!(rfile = fopen(recfile,"w"))){
  192.             HUP puts("Cannot write record file\n");
  193.             goto unlock;
  194.         }
  195.  
  196.         if(!done_stopprint) if(rank0 > 0){
  197.             if(rank0 <= 10)
  198.             puts("You made the top ten list!\n");
  199.             else
  200.         printf("You reached the %d%s place on the top %d list.\n\n",
  201.             rank0, ordin(rank0), ENTRYMAX);
  202.         }
  203.     }
  204.     if(rank0 == 0) rank0 = rank1;
  205.     if(rank0 <= 0) rank0 = rank;
  206.     if(!done_stopprint) outheader();
  207.     t1 = tt_head;
  208.     for(rank = 1; t1->points != 0; rank++, t1 = t1->tt_next) {
  209.       if(flg) fprintf(rfile,"%6s %d %d %d %d %d %ld %c%c %s,%s\n",
  210.         t1->date, t1->uid,
  211.         t1->level, t1->maxlvl,
  212.         t1->hp, t1->maxhp, t1->points,
  213.         t1->plchar, t1->sex, t1->name, t1->death);
  214.       if(done_stopprint) continue;
  215.       if(rank > flags.end_top &&
  216.         (rank < rank0-flags.end_around || rank > rank0+flags.end_around)
  217.         && (!flags.end_own ||
  218. #ifdef PERS_IS_UID
  219.                   t1->uid != t0->uid
  220. #else
  221.                   strncmp(t1->name, t0->name, NAMSZ)
  222. #endif
  223.         )) continue;
  224.       if(rank == rank0-flags.end_around &&
  225.          rank0 > flags.end_top+flags.end_around+1 &&
  226.          !flags.end_own)
  227.         (void) putchar('\n');
  228.       if(rank != rank0)
  229.         (void) outentry(rank, t1, 0);
  230.       else if(!rank1)
  231.         (void) outentry(rank, t1, 1);
  232.       else {
  233.         int t0lth = outentry(0, t0, -1);
  234.         int t1lth = outentry(rank, t1, t0lth);
  235.         if(t1lth > t0lth) t0lth = t1lth;
  236.         (void) outentry(0, t0, t0lth);
  237.       }
  238.     }
  239.     if(rank0 >= rank) if(!done_stopprint)
  240.         (void) outentry(0, t0, 1);
  241.     (void) fclose(rfile);
  242. unlock: ;
  243. #ifdef UNIX
  244.     (void) unlink(reclock);
  245. #endif
  246. }
  247.  
  248. outheader() {
  249. char linebuf[BUFSZ];
  250. register char *bp;
  251. #ifdef KJSMODS
  252.     (void) strcpy(linebuf, " No  Points    Name");
  253. #else
  254.     (void) strcpy(linebuf, "Number Points  Name");
  255. #endif
  256.     bp = eos(linebuf);
  257.     while(bp < linebuf + COLNO - 9) *bp++ = ' ';
  258.     (void) strcpy(bp, "Hp [max]");
  259.     puts(linebuf);
  260. }
  261.  
  262. /* so>0: standout line; so=0: ordinary line; so<0: no output, return lth */
  263. int
  264. outentry(rank,t1,so) register struct toptenentry *t1; {
  265. boolean quit = FALSE, killed = FALSE, starv = FALSE;
  266. char linebuf[BUFSZ];
  267.     linebuf[0] = 0;
  268.     if(rank) Sprintf(eos(linebuf), "%3d", rank);
  269.         else Sprintf(eos(linebuf), "   ");
  270. #ifdef KJSMODS
  271.     Sprintf(eos(linebuf), " %7ld %10s", t1->points, t1->name);
  272. #else
  273. # ifdef DGKMOD
  274.     Sprintf(eos(linebuf), " %6ld %10s", t1->points, t1->name);
  275. # else
  276.     Sprintf(eos(linebuf), " %6ld %8s", t1->points, t1->name);
  277. # endif
  278. #endif
  279.     if(t1->plchar == 'X') Sprintf(eos(linebuf), " ");
  280.     else Sprintf(eos(linebuf), "-%c ", t1->plchar);
  281.     if(!strncmp("escaped", t1->death, 7)) {
  282.       if(!strcmp(" (with amulet)", t1->death+7))
  283.         Sprintf(eos(linebuf), "escaped the dungeon with amulet");
  284.       else
  285.         Sprintf(eos(linebuf), "escaped the dungeon [max level %d]",
  286.           t1->maxlvl);
  287.     } else {
  288.       if(!strncmp(t1->death,"quit",4)) {
  289.         quit = TRUE;
  290. #ifndef KJSMODS
  291.         if(t1->maxhp < 3*t1->hp && t1->maxlvl < 4)
  292.         Sprintf(eos(linebuf), "cravenly gave up");
  293.         else
  294. #endif
  295.         Sprintf(eos(linebuf), "quit");
  296.       }
  297.       else if(!strcmp(t1->death,"choked"))
  298.         Sprintf(eos(linebuf), "choked on %s food",
  299.         (t1->sex == 'F') ? "her" : "his");
  300.       else if(!strncmp(t1->death,"starv",5))
  301.         Sprintf(eos(linebuf), "starved to death"), starv = TRUE;
  302.       else Sprintf(eos(linebuf), ", killed"), killed = TRUE;
  303.       Sprintf(eos(linebuf), " on%s level %d",
  304.         (killed || starv) ? "" : " dungeon", t1->level);
  305.       if(t1->maxlvl != t1->level)
  306.         Sprintf(eos(linebuf), " [max %d]", t1->maxlvl);
  307.       if(quit && t1->death[4]) Sprintf(eos(linebuf), t1->death + 4);
  308.     }
  309.     if(killed) Sprintf(eos(linebuf), " by %s%s",
  310.       (!strncmp(t1->death, "trick", 5) || !strncmp(t1->death, "the ", 4))
  311.         ? "" :
  312.       index(vowels,*t1->death) ? "an " : "a ",
  313.       t1->death);
  314.     Sprintf(eos(linebuf), ".");
  315.     if(t1->maxhp) {
  316.       register char *bp = eos(linebuf);
  317.       char hpbuf[10];
  318.       int hppos;
  319. #ifdef KJSMODS
  320.       int lngr = strlen(linebuf);
  321. #endif
  322.       Sprintf(hpbuf, (t1->hp > 0) ? itoa(t1->hp) : "-");
  323.       hppos = COLNO - 7 - strlen(hpbuf);
  324. #ifdef KJSMODS
  325.       if (lngr >= hppos) hppos = (2*COLNO) - 7 - strlen(hpbuf);
  326. #endif
  327.       if(bp <= linebuf + hppos) {
  328.         /* pad any necessary blanks to the hit point entry */
  329.         while(bp < linebuf + hppos) *bp++ = ' ';
  330.         (void) strcpy(bp, hpbuf);
  331.         Sprintf(eos(bp), " [%d]", t1->maxhp);
  332.       }
  333.     }
  334.     if(so == 0) puts(linebuf);
  335.     else if(so > 0) {
  336.       register char *bp = eos(linebuf);
  337.       if(so >= COLNO) so = COLNO-1;
  338.       while(bp < linebuf + so) *bp++ = ' ';
  339.       *bp = 0;
  340.       standoutbeg();
  341. #ifdef stdout_puts    /* For AMIGA */
  342.       stdout_puts(linebuf);
  343. #else
  344.       fputs(linebuf,stdout);
  345. #endif
  346.       standoutend();
  347.       (void) putchar('\n');
  348.     }
  349.     return(strlen(linebuf));
  350. }
  351.  
  352. char *
  353. itoa(a) int a; {
  354. static char buf[12];
  355.     Sprintf(buf,"%d",a);
  356.     return(buf);
  357. }
  358.  
  359. char *
  360. ordin(n) int n; {
  361. register int d = n%10;
  362.     return((d==0 || d>3 || n/10==1) ? "th" : (d==1) ? "st" :
  363.         (d==2) ? "nd" : "rd");
  364. }
  365.  
  366. char *
  367. eos(s)
  368. register char *s;
  369. {
  370.     while(*s) s++;
  371.     return(s);
  372. }
  373.  
  374. /*
  375.  * Called with args from main if argc >= 0. In this case, list scores as
  376.  * requested. Otherwise, find scores for the current player (and list them
  377.  * if argc == -1).
  378.  */
  379. prscore(argc,argv) int argc; char **argv; {
  380.     extern char *hname;
  381.     char **players;
  382.     int playerct;
  383.     int rank;
  384.     register struct toptenentry *t1, *t2;
  385.     char *recfile = RECORD;
  386.     FILE *rfile;
  387.     register flg = 0;
  388.     register int i;
  389. #ifdef nonsense
  390.     long total_score = 0L;
  391.     char totchars[10];
  392.     int totcharct = 0;
  393. #endif
  394.     int outflg = (argc >= -1);
  395. #ifdef PERS_IS_UID
  396.     int uid = -1;
  397. #else
  398.     char *player0;
  399. #endif
  400. #ifdef AMIGA
  401.     curx = FAR; /* Prevent screen from closing too fast */
  402. #endif
  403.  
  404.     if(!(rfile = fopen(recfile,"r"))){
  405.         puts("Cannot open record file!");
  406.         return;
  407.     }
  408.  
  409.     if(argc > 1 && !strncmp(argv[1], "-s", 2)){
  410.         if(!argv[1][2]){
  411.             argc--;
  412.             argv++;
  413.         } else if(!argv[1][3] && index("CFKSTWX", argv[1][2])) {
  414.             argv[1]++;
  415.             argv[1][0] = '-';
  416.         } else    argv[1] += 2;
  417.     }
  418.     if(argc <= 1){
  419. #ifdef PERS_IS_UID
  420.         uid = getuid();
  421.         playerct = 0;
  422. #else
  423.         player0 = plname;
  424.         if(!*player0)
  425.             player0 = "hackplayer";
  426.         playerct = 1;
  427.         players = &player0;
  428. #endif
  429.     } else {
  430.         playerct = --argc;
  431.         players = ++argv;
  432.     }
  433.     if(outflg) putchar('\n');
  434.  
  435.     t1 = tt_head = newttentry();
  436.     for(rank = 1; ; rank++) {
  437.       if(fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
  438.         t1->date, &t1->uid,
  439.         &t1->level, &t1->maxlvl,
  440.         &t1->hp, &t1->maxhp, &t1->points,
  441.         &t1->plchar, &t1->sex, t1->name, t1->death) != 11)
  442.             t1->points = 0;
  443.       if(t1->points == 0) break;
  444. #ifdef PERS_IS_UID
  445.       if(!playerct && t1->uid == uid)
  446.         flg++;
  447.       else
  448. #endif
  449.       for(i = 0; i < playerct; i++){
  450.         if(strcmp(players[i], "all") == 0 ||
  451.            strncmp(t1->name, players[i], NAMSZ) == 0 ||
  452.           (players[i][0] == '-' &&
  453.            players[i][1] == t1->plchar &&
  454.            players[i][2] == 0) ||
  455.           (digit(players[i][0]) && rank <= atoi(players[i])))
  456.             flg++;
  457.       }
  458.       t1 = t1->tt_next = newttentry();
  459.     }
  460.     (void) fclose(rfile);
  461.     if(!flg) {
  462.         if(outflg) {
  463.         printf("Cannot find any entries for ");
  464.         if(playerct < 1) printf("you.\n");
  465.         else {
  466.           if(playerct > 1) printf("any of ");
  467.           for(i=0; i<playerct; i++)
  468.             printf("%s%s", players[i], (i<playerct-1)?", ":".\n");
  469.           printf("Call is: %s -s [playernames]\n", hname);
  470.         }
  471.         }
  472.         return;
  473.     }
  474.  
  475.     if(outflg) outheader();
  476.     t1 = tt_head;
  477.     for(rank = 1; t1->points != 0; rank++, t1 = t2) {
  478.         t2 = t1->tt_next;
  479. #ifdef PERS_IS_UID
  480.         if(!playerct && t1->uid == uid)
  481.             goto outwithit;
  482.         else
  483. #endif
  484.         for(i = 0; i < playerct; i++){
  485.             if(strcmp(players[i], "all") == 0 ||
  486.                strncmp(t1->name, players[i], NAMSZ) == 0 ||
  487.               (players[i][0] == '-' &&
  488.                players[i][1] == t1->plchar &&
  489.                players[i][2] == 0) ||
  490.               (digit(players[i][0]) && rank <= atoi(players[i]))){
  491.             outwithit:
  492.                 if(outflg)
  493.                     (void) outentry(rank, t1, 0);
  494. #ifdef nonsense
  495.                 total_score += t1->points;
  496.                 if(totcharct < sizeof(totchars)-1)
  497.                     totchars[totcharct++] = t1->plchar;
  498. #endif
  499.                 break;
  500.             }
  501.         }
  502.         free((char *) t1);
  503.     }
  504. #ifdef nonsense
  505.     totchars[totcharct] = 0;
  506.  
  507.     /* We would like to determine whether he is experienced. However,
  508.        the information collected here only tells about the scores/roles
  509.        that got into the topten (top 100?). We should maintain a
  510.        .hacklog or something in his home directory. */
  511.     flags.beginner = (total_score < 6000);
  512.     for(i=0; i<6; i++)
  513.         if(!index(totchars, "CFKSTWX"[i])) {
  514.         flags.beginner = 1;
  515.         if(!pl_character[0]) pl_character[0] = "CFKSTWX"[i];
  516.         break;
  517.     }
  518. #endif /* nonsense /**/
  519. }
  520.